package com.supersit.GZFDA.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.supersit.GZFDA.entity.Business;
import com.supersit.GZFDA.entity.UnUpLoad;

public class DBUtils {
	/**
	 * 判断数据库是否存在该公司
	 * 
	 * @param QYMC
	 * @param context
	 * @return
	 */
	public static boolean isExist(String QYMC, Context context) {
		boolean flag;
		GZFDASqliteDBHelper dpDbHelper = new GZFDASqliteDBHelper(context);
		SQLiteDatabase db = dpDbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select * from unupload where business_name = ?",
				new String[] { QYMC });

		flag = cursor.moveToNext();
		cursor.close();
		db.close();
		return flag;

	}

	public static int deleteUnUpLoad(String QYMC, Context context) {
		int num;
		GZFDASqliteDBHelper dpDbHelper = new GZFDASqliteDBHelper(context);
		SQLiteDatabase db = dpDbHelper.getReadableDatabase();
		num = db.delete("unupload", "business_name = ?", new String[] { QYMC });
		db.close();
		return num;
	}

	/**
	 * 获取未上传的数据
	 * 
	 * @param context
	 * @return
	 */
	public static List<UnUpLoad> getUnUpLoadData(Context context) {
		List<UnUpLoad> list = new ArrayList<UnUpLoad>();
		GZFDASqliteDBHelper dpDbHelper = new GZFDASqliteDBHelper(context);
		SQLiteDatabase db = dpDbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select *from  unupload", null);
		while (cursor.moveToNext()) {
			UnUpLoad u = new UnUpLoad();
			String QYMC;
			String X;
			String Y;
			String CJR;
			String CJSJ;
			String BZ;
			String isupload;
			String photos;
			String street;
			String community;
			String manualMode;
			QYMC = cursor.getString(cursor.getColumnIndex("business_name")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("business_name"));
			X = cursor.getString(cursor.getColumnIndex("coords_x")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("coords_x"));
			Y = cursor.getString(cursor.getColumnIndex("coords_y")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("coords_y"));

			CJR = cursor.getString(cursor.getColumnIndex("upuser")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("upuser"));

			CJSJ = cursor.getString(cursor.getColumnIndex("uploaddate")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("uploaddate"));

			BZ = cursor.getString(cursor.getColumnIndex("BY")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("BY"));

			isupload = cursor.getString(cursor.getColumnIndex("isupload")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("isupload"));

			photos = cursor.getString(cursor.getColumnIndex("photos")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("photos"));

			street = cursor.getString(cursor.getColumnIndex("street")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("street"));

			community = cursor.getString(cursor.getColumnIndex("community")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("community"));
			manualMode = cursor.getString(cursor.getColumnIndex("manualmode")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("manualmode"));
			u.setBZ(BZ);
			u.setCJR(CJR);
			u.setCJSJ(CJSJ);
			u.setQYMC(QYMC);
			u.setPhotos(photos);
			u.setIsupload(isupload);
			u.setX(X);
			u.setY(Y);
			u.setStreet(street);
			u.setCommunity(community);
			u.setManualMode(manualMode);
			if ("-1".equals(u.getIsupload())) {
				list.add(u);
			}

		}
		cursor.close();
		db.close();
		return list;

	}

	/**
	 * 把数据库未上传的文件改为已上传
	 * 
	 * @param QYMC
	 * @param context
	 */
	public static void setUpLoaded(String QYMC, Context context) {
		ContentValues updatedValues = new ContentValues();
		updatedValues.put("isupload", "0");
		String where = "business_name='" + QYMC + "'";
		GZFDASqliteDBHelper dpDbHelper = new GZFDASqliteDBHelper(context);
		SQLiteDatabase db = dpDbHelper.getReadableDatabase();
		db.update("unupload", updatedValues, where, null);
		db.close();
	}

	/**
	 * 通过企业名称查询企业实体
	 * 
	 * @param Business_name
	 * @param context
	 * @return
	 */
	public static Business getBusiness(String Business_name, Context context) {
		SQLiteDatabase db = LoadDBData.openDatabase(context);
		Cursor cursor = db.rawQuery(
				"select * from gzfad_data where business_name=?",
				new String[] { Business_name });
		Business business = new Business();
		String id = "";
		String business_name = "";
		String license_key = "";
		String register_address = "";
		String operate_addrress = "";
		String contacts = "";
		String legal_person = "";
		String busi = "";
		String quality_principal = "";
		String range = "";
		String expiry_data = "";
		String data_of_issue = "";
		String street = "";
		String business_type = "";
		String isUpload = "";

		while (cursor.moveToNext()) {
			id = cursor.getString(cursor.getColumnIndex("id")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("id"));

			business_name = cursor.getString(cursor
					.getColumnIndex("business_name")) == null ? "" : cursor
					.getString(cursor.getColumnIndex("business_name"));
			license_key = cursor
					.getString(cursor.getColumnIndex("license_key")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("license_key"));
			register_address = cursor.getString(cursor
					.getColumnIndex("register_address")) == null ? "" : cursor
					.getString(cursor.getColumnIndex("register_address"));
			operate_addrress = cursor.getString(cursor
					.getColumnIndex("operate_address")) == null ? "" : cursor
					.getString(cursor.getColumnIndex("operate_address"));
			contacts = cursor.getString(cursor.getColumnIndex("contacts")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("contacts"));
			legal_person = cursor.getString(cursor
					.getColumnIndex("legal_person")) == null ? "" : cursor
					.getString(cursor.getColumnIndex("legal_person"));
			busi = cursor.getString(cursor.getColumnIndex("busi")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("busi"));
			quality_principal = cursor.getString(cursor
					.getColumnIndex("quality_principal")) == null ? "" : cursor
					.getString(cursor.getColumnIndex("quality_principal"));
			range = cursor.getString(cursor.getColumnIndex("range")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("range"));
			expiry_data = cursor
					.getString(cursor.getColumnIndex("expiry_date")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("expiry_date"));
			data_of_issue = cursor.getString(cursor
					.getColumnIndex("date_of_issue")) == null ? "" : cursor
					.getString(cursor.getColumnIndex("date_of_issue"));
			street = cursor.getString(cursor.getColumnIndex("street")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("street"));
			business_type = cursor.getString(cursor
					.getColumnIndex("business_type")) == null ? "" : cursor
					.getString(cursor.getColumnIndex("business_type"));
			isUpload = cursor.getString(cursor.getColumnIndex("is_upload")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("is_upload"));
		}
		business.setBusi(busi);
		business.setBusiness_name(business_name);
		business.setBusiness_type(business_type);
		business.setContacts(contacts);
		business.setData_of_issue(data_of_issue);
		business.setExpiry_data(expiry_data);
		business.setId(id);
		business.setIsUpload(isUpload);
		business.setLegal_person(legal_person);
		business.setLicense_key(license_key);
		business.setOperate_addrress(operate_addrress);
		business.setQuality_principal(quality_principal);
		business.setRange(range);
		business.setRegister_address(register_address);
		business.setStreet(street);
		cursor.close();
		db.close();
		return business;

	}
}
